<template>
  <div class="bg-form">
    <el-form
      :model="addForm"
      label-width="130px"
      :show-message="false"
      :rules="addFormRules"
      ref="addForm"
    >
      <el-row>
        <el-col>采购基础信息</el-col>
      </el-row>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <el-col :span="6">
          <el-form-item label="配车合同号:" prop="contractNum">
            <el-input
              disabled
              v-model="addForm.contractNum"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="采购门店:" prop="storeName">
            <el-input disabled v-model="addForm.storeName" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车顶颜色:" prop="roofColor">
            <el-input disabled v-model="addForm.roofColor" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车身颜色:" prop="outColor">
            <el-input disabled v-model="addForm.outColor" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="内饰颜色:" prop="inColor">
            <el-input disabled v-model="addForm.inColor" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="品 牌:" prop="brandName">
            <el-input disabled v-model="addForm.brandName" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="车 系:" prop="seriesName">
            <el-input
              disabled
              v-model="addForm.seriesName"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="车 型:" prop="modelName">
            <el-input disabled v-model="addForm.modelName" clearable></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="标准进价:" prop="criterionPrice">
            <el-input
              disabled
              v-model="addForm.criterionPrice"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实际进价:" prop="realityPrice">
            <el-input
              disabled
              v-model="addForm.realityPrice"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="资金来源:" prop="capitalSourceDictId">
            <el-select
              class="w-100"
              clearable
              disabled
              multiple
              v-model="addForm.capitalSourceDictId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in dict['资金来源']"
                :key="item.index"
                :value="item.value"
                :label="item.text"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>采购确认信息</el-col>
      </el-row>
      <el-divider content-position="center"></el-divider>
      <el-row>
        <el-col :span="24">
          <el-row>
            <el-col :span="8">
              <el-form-item label="资金利息:" prop="zjlxId">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.zjlxId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in moneyList"
                    :key="item.id"
                    :value="item.id"
                    :label="item.name"
                  />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="计息起始日期:" prop="interestStartTime">
                <el-date-picker
                  v-model="addForm.interestStartTime"
                  type="date"
                  class="w-100"
                  placeholder="计息起始日期"
                  clearable
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="贷款到期日期:" prop="loanEndTime">
                <el-date-picker
                  v-model="addForm.loanEndTime"
                  type="date"
                  class="w-100"
                  placeholder="贷款到期日期"
                  clearable
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="提车时间:" prop="deliveryTime">
                <el-date-picker
                  v-model="addForm.deliveryTime"
                  type="date"
                  class="w-100"
                  placeholder="提车时间"
                  clearable
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="发动机号:" prop="engineModel">
                <el-input
                  placeholder="发动机号"
                  v-model="addForm.engineModel"
                />
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="车架号:" prop="vin">
                <el-input placeholder="车架号" v-model="addForm.vin" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="购车方式:" prop="buyCarType">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.buyCarType"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in dict['购车方式']"
                    :key="item.index"
                    :value="item.value"
                    :label="item.text"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="仓 库:" prop="storageId">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.storageId"
                  placeholder="请选择"
                  @change="getStorageSite"
                >
                  <el-option
                    v-for="item in storageList"
                    :key="item.id"
                    :value="item.id"
                    :label="item.storageName"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="仓 位:" prop="storageSiteId">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.storageSiteId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in storageSitList"
                    :key="item.id"
                    :value="item.id"
                    :label="item.storageSiteName"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="排放标准:" prop="emissionStandards">
                <el-select
                  class="w-100"
                  clearable
                  v-model="addForm.emissionStandards"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in dict['排放标准']"
                    :key="item.index"
                    :value="item.value"
                    :label="item.text"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              <el-form-item label="备注:" prop="remark">
                <el-input v-model="addForm.remark" placeholder="请输入" type="textarea" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" align="right">
              <el-button
                type="success"
                v-permission="'purchaseWh:update'"
                @click="onSubmit('addForm')"
                >保存</el-button
              >
            </el-col>
          </el-row>
        </el-col>

        <!-- <el-col :span="6"> 上传 </el-col> -->
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getDict } from '@/utils/index'
import {
  save,
  details,
} from '@/api/pre-sale/newCarStock/putInStorageManagement/purchaseInventory'
import { findAllByStoreId } from '@/api/pre-sale/newCarStock/capitalInterest'
import { findByStore } from '@/api/after-sale/storehouseManage/warehouseManage'
import { storageSite } from '@/api/pre-sale/boutiqueStock/inventoryCheck'
export default {
  props: ['id'],
  data() {
    return {
      addForm: {},
      form: {},
      addFormRules: {
        deliveryTime: [
          { required: true, message: '请选择', trigger: 'change' },
        ],
        buyCarType: [{ required: true, message: '请选择', trigger: 'change' }],
        emissionStandards: [
          { required: true, message: '请选择', trigger: 'change' },
        ],
      },
      storageList: [],
      storageSitList: [],
      moneyList: [],
      dict: {},
    }
  },
  mounted() {
    this.dict = getDict(['排放标准', '购车方式', '资金来源'])
    this.getDetail()
  },
  methods: {
    // 获取仓库
    async getWareHouse(id) {
      let { data } = await findByStore(id)
      this.storageList = data
    },
    // 获取仓位
    async getStorageSite(value) {
      const param = {
        storageId: value,
        storeId: this.addForm.storeId,
      }
      let { data } = await storageSite(param)
      this.storageSitList = data.content || []
    },
    //资金利息
    async findAllByStoreId(id) {
      const { data } = await findAllByStoreId(id)
      this.moneyList = data
    },
    // 保存
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // const { storageId, storageSiteId, emissionStandards } = this.addForm
          await save(this.addForm)
          this.$router.push({
            name: 'purchaseInventory',
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async getDetail() {
      const { success, data } = await details(this.id)

      if (success) {
        this.getWareHouse(data.storeId)
        this.findAllByStoreId(data.storeId)
        data.capitalSourceDictId=data.capitalSourceDictId.split(',').map(item=>parseInt(item))
        this.addForm = { ...this.addForm, ...data }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.bg-form {
  background: #ffffff;
  padding: 20px;
}
</style>

